﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;
using TMDT_Nhom1A.BUS;
using System.Collections.Generic;
using System.Drawing;

namespace TMDT_Nhom1A.UserControl_Admin
{
    public partial class BankWebserviceManager : System.Web.UI.UserControl
    {
        private static List<BANK_WEBSERVICE> bankWsInfo = new List<BANK_WEBSERVICE>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bankWsInfo = BankWebservice_BUS.getAllBankWebservice();
                gvBankWSList.DataSource = bankWsInfo;
                gvBankWSList.DataBind();
            }
        }

        protected void btAdd_Click(object sender, ImageClickEventArgs e)
        {
            List<BANK_WEBSERVICE> temp = new List<BANK_WEBSERVICE>();
            BANK_WEBSERVICE bankWs = new BANK_WEBSERVICE();
            bankWs.Bank_WS_ID = "";

            temp.Add(bankWs);
            DlAddUpdateBankWS.EditItemIndex = -1;
            DlAddUpdateBankWS.DataSource = temp;
            DlAddUpdateBankWS.DataBind();
            DlAddUpdateBankWS.Visible = true;

        }

        protected void gvBankWSList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string bankWS_ID = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                //tim thong tin trong list
                List<BANK_WEBSERVICE> temp = new List<BANK_WEBSERVICE>();
                for (int i = 0; i < bankWsInfo.Count; i++)
                {
                    if (bankWsInfo[i].Bank_WS_ID == bankWS_ID)
                    {
                        temp.Add(bankWsInfo[i]);
                    }
                }

                DlAddUpdateBankWS.EditItemIndex = 0;
                DlAddUpdateBankWS.Visible = true;
                DlAddUpdateBankWS.DataSource = temp;
                DlAddUpdateBankWS.DataBind();               


            }

            if (e.CommandName == "btDelete")
            {
                
                bool result = BankWebservice_BUS.deleteBankWebservice(bankWS_ID);
                if (result == true)
                {
                    for (int i = 0; i < bankWsInfo.Count; i++)
                    {
                        if (bankWsInfo[i].Bank_WS_ID == bankWS_ID)
                        {
                            bankWsInfo.RemoveAt(i);
                        }
                    }
                    //refesh lai gridview va hien thong bao
                    gvBankWSList.DataSource = BankWebservice_BUS.getAllBankWebservice();
                    gvBankWSList.DataBind();
                }
                else
                {
                    LbThongBao.Text = "Xoá Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }
        }

        protected void gvBankWSList_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void DlAddUpdateBankWS_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string bankWS_ID = e.CommandArgument.ToString();

            if (e.CommandName == "Add")
            {
                TextBox tbBankID = (TextBox)e.Item.FindControl("tbBankID");
                TextBox tblink = (TextBox)e.Item.FindControl("tblink");
                TextBox tbLinkWebservice = (TextBox)e.Item.FindControl("tbLinkWebservice");
                TextBox tbBankName = (TextBox)e.Item.FindControl("tbBankName");
                TextBox tbBankDescription = (TextBox)e.Item.FindControl("tbBankDescription");

                BANK_WEBSERVICE bankWs = new BANK_WEBSERVICE();
                bankWs.Bank_WS_ID = tbBankID.Text;
                bankWs.BankUrl = tblink.Text;
                bankWs.BankWebservice_Url = tbLinkWebservice.Text;
                bankWs.BankName = tbBankName.Text;
                bankWs.Description = tbBankDescription.Text;

                bool result = BankWebservice_BUS.insertBankWebservice(bankWs);
                if (result == true)
                {
                    bankWsInfo.Add(bankWs);
                    //refesh lai gridview va hien thong bao
                    gvBankWSList.DataSource = BankWebservice_BUS.getAllBankWebservice();
                    gvBankWSList.DataBind();
                    DlAddUpdateBankWS.Visible = false;

                }
                else
                {
                    LbThongBao.Text = "Thêm Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }

            if (e.CommandName == "Update")
            {
                TextBox tblink = (TextBox)e.Item.FindControl("tblink");
                TextBox tbLinkWebservice = (TextBox)e.Item.FindControl("tbLinkWebservice");
                TextBox tbBankName = (TextBox)e.Item.FindControl("tbBankName");
                TextBox tbBankDescription = (TextBox)e.Item.FindControl("tbBankDescription");

                BANK_WEBSERVICE newBankWS = new BANK_WEBSERVICE();
                newBankWS.Bank_WS_ID = bankWS_ID;
                newBankWS.BankUrl = tblink.Text;
                newBankWS.BankWebservice_Url = tbLinkWebservice.Text;
                newBankWS.BankName = tbBankName.Text;
                newBankWS.Description = tbBankDescription.Text;

                bool result = BankWebservice_BUS.updateBankWebservice(newBankWS);
                if (result == true)
                {
                    //refesh lai gridview va hien thong bao
                    gvBankWSList.DataSource = BankWebservice_BUS.getAllBankWebservice();
                    gvBankWSList.DataBind();
                }
                else
                {
                    LbThongBao.Text = "Cập Nhật Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }

            if (e.CommandName == "Cancel")
            {
                DlAddUpdateBankWS.Visible = false;
            }
        }

        
    }
}